////
////  HTTPClient.swift
////  sinazd
////
////  Created by apple on 16/3/15.
////  Copyright © 2016年 apple. All rights reserved.
////
//
import UIKit
import AFNetworking

private let hostName = "https://api.weibo.com/"
//private let hostname = "https://.dev.api.weibo.com/"
private let domain = "com.baidu.data.error"

enum HTTPMethods:String {
    
    case POST = "POST"
    case GET = "GET"
    
}

class HTTPClient: AFHTTPSessionManager {
    
    
    static let sharedHTTPClient:HTTPClient = {
        
        let url = NSURL(string: hostName)
        
        let tools = HTTPClient(baseURL:url)
        
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
        
    }()
    
    
    func requestJSONDict(method:HTTPMethods,urlString:String,parameters:[String:AnyObject],finished:(dict:[String:AnyObject]?,error:NSError?) -> ()) {
        
        if method == .POST {
            
            POST(urlString, parameters: parameters, progress: nil, success: { (_, result) -> Void in
                
                guard let dict = result as? [String:AnyObject] else {
                    
                    let myError = NSError(domain: domain, code: 10001, userInfo: [NSLocalizedDescriptionKey : "数据结构错误"])
                    
                    finished(dict: nil,error: myError)
                    return
                }
                
                finished(dict: dict, error: nil)
                
                
                }, failure: { (_, error) -> Void in
                    
                    
                    finished(dict: nil, error:error )
                    
            })
            
        }else{
            
            GET(urlString, parameters: parameters, progress: nil, success: { (_, result) -> Void in
                
                guard let dict = result as? [String:AnyObject] else {
                    
                    let myError = NSError(domain: domain, code: 10001, userInfo: [NSLocalizedDescriptionKey : "数据结构错误"])
                    
                    finished(dict: nil,error: myError)
                    return
                    
                }
                finished(dict: dict, error: nil)
                
                }, failure: { (_, error) -> Void in
                    
                    finished(dict: nil, error:error )
                    
            })
            
        }
        
        
        
        
    }
    
}